/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.globussoft.xml;

/**
 *
 * @author Dj
 */
import java.io.FileOutputStream;
import java.io.IOException;


import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Text;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

import java.io.FileNotFoundException;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.OutputFormat;
public class XmlGeneralStore
{

    private boolean Quota_Warning;
     private boolean Status;
     private int Alert_Days;
     Document dom=null;
     private String projectName="Pomblast";
     public  XmlGeneralStore()
     {
         projectName=SettingsXml.getProjectName();
     }


      public boolean getQuotaWarning() {

            return Quota_Warning;

      }
    public void setQuotaWarning(boolean Quota_Warning) {

            this.Quota_Warning = Quota_Warning;
         }

      public boolean getStatus() {

            return Status;

      }

      public void setStatus(boolean Status) {

            this.Status = Status;

      }

      public int getAlertDays() {

            return Alert_Days;

      }

      public void setAlertDays(int Alert_Days) {

            this.Alert_Days = Alert_Days;

      }


       public  void writeGeneralXml(String filename) throws ParserConfigurationException{

           //create a list to hold the data
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

            try {

                  DocumentBuilder db = dbf.newDocumentBuilder();

                  dom = db.newDocument();

                  Element rootElement = dom.createElement("General-Settings");

                 // dom.appendChild(rootElement);

                  Element generalElement = dom.createElement("General");

                  generalElement=createGeneralElement(generalElement,"Status",""+Status);
                  generalElement=createGeneralElement(generalElement,"Quota_Warning",""+Quota_Warning);
                  generalElement=createGeneralElement(generalElement,"Alert_Days",""+Alert_Days);
                  rootElement.appendChild(generalElement);
                  dom.appendChild(rootElement);

                 try{
                  printToFile(dom,filename);
                 }catch(Exception ex){ex.printStackTrace();}



            }catch(ParserConfigurationException pce) {
                pce.printStackTrace();

                //  System.out.println("Error while trying to instantiate DocumentBuilder " + pce);

                  System.exit(1);

             }


      }

               /**
	 * Helper method which creates a XML element
	 * @param xmlgeneralstore The SetXmlGeneralStore for which we need to create an xml representation
	 * @return XML element snippet representing a SetXmlGeneralStore
	 */

      private Element createGeneralElement(Element generalElement,String property,String value)
      {


          //create Status element and Status text node and attach it to generalElement
            Element statusElement = dom.createElement(property);

            Text statusText = dom.createTextNode(value);

            statusElement.appendChild(statusText);

            generalElement.appendChild(statusElement);


            return generalElement;

      }

       private void printToFile(Document dom,String filename) throws FileNotFoundException, IOException{
           try
            {

                 OutputFormat format = new OutputFormat(dom);
                  format.setIndenting(true);
                  //URL mySource = XmlGeneralStore.class.getProtectionDomain().getCodeSource().getLocation();
                //  File sourceFile = new File(".");
                 boolean success;
                 String    source=System.getProperty("user.home");
                 String dir=source+"\\"+projectName;
                 File sourceFile = new File(dir);
                 if(!sourceFile.exists())
                 {
                     success=new File(dir).mkdir();
                 }


                // attach text file to application directory(.class file)
                  String xmlfile = dir+"\\"+filename;

               //to generate output to console use this serializer

              //XMLSerializer serializer = new XMLSerializer(System.out, format);

             //to generate a file output use fileoutputstream instead of system.out

                  XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File(xmlfile)), format);
                  serializer.serialize(dom);

              } catch(IOException ie)
              {
                 ie.printStackTrace();
              }

      }

       //reading the xml file code her
public void ReadGeneralXml(String filename)
{
   // String storegenerralxml[]=new String[10];
  try {
      // URL mySource = XmlGeneralStore.class.getProtectionDomain().getCodeSource().getLocation();
     // File sourceFile = new File(".");
     // attach text file to application directory(.class file)
      //String xmlfile = sourceFile.getAbsolutePath()+"\\"+"generalsettings.xml";
	  boolean success;
      String    source=System.getProperty("user.home");
      String dir=source+"\\"+projectName;
      File fXmlFile = new File(dir+"\\"+filename);
      if(fXmlFile.isFile())
      {
	  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
	  Document doc = dBuilder.parse(fXmlFile);
	  doc.getDocumentElement().normalize();

	 // System.out.println("General Settings :" + doc.getDocumentElement().getNodeName());
	  NodeList nList = doc.getElementsByTagName("General");
	//  System.out.println("-----------------------");

	  for (int temp = 0; temp < nList.getLength(); temp++) {

	    Node nNode = nList.item(temp);

	    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

	      Element eElement = (Element) nNode;

//	     storegenerralxml[0]= getTagValue("Status",eElement);
//	     storegenerralxml[1]= getTagValue("Quota_Warning",eElement);
//	     storegenerralxml[2]=getTagValue("Alert_Days",eElement).trim();
         String Status1=getTagValue("Status",eElement).trim();
         String Quota_Warning1= getTagValue("Quota_Warning",eElement);
         if(Status1.equals("false"))
             Status=false;
         else
             Status=true;
          if(Quota_Warning1.equals("false"))
             Quota_Warning=false;
          else
              Quota_Warning=true;
        String Alert_Days1=getTagValue("Alert_Days",eElement);

            Alert_Days=Integer.parseInt(Alert_Days1);

	    }
      }
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
    //return storegenerralxml;
}

private static String getTagValue(String sTag, Element eElement)
 {
	  NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
	  Node nValue = (Node) nlList.item(0);

	  return nValue.getNodeValue();

 }

}